package com.hzhr.iot.user.convertor.custom;

import com.hzhr.iot.user.convertor.ResourceConvertor;
import com.hzhr.iot.user.entity.ext.ResourceEntityExt;
import com.hzhr.iot.user.entity.ext.ResourceInActionEntityExt;
import com.hzhr.iot.user.model.resource.ResourceModel;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceCustomConvertor extends ResourceConvertor {

    @Override
    public ResourceModel ext2resourceModel(ResourceEntityExt in) {
        ResourceModel result = super.ext2resourceModel(in);

        List<ResourceInActionEntityExt> resourceInActionEntityExtList = in.getResourceInActionExtList();
        if (resourceInActionEntityExtList != null && resourceInActionEntityExtList.size() > 0) {
            if (resourceInActionEntityExtList.size() == 1) {
                result.setMask(resourceInActionEntityExtList.get(0).getMask());
            } else {
                String mask = "00000";
                for (ResourceInActionEntityExt resourceInActionEntityExt:
                        resourceInActionEntityExtList) {
                    String currentMask = resourceInActionEntityExt.getMask();
                    for (int i = 0; i < 5; i++) {
                        char ci = currentMask.charAt(i);
                        if (ci != '0') {
                            mask = mask.substring(0, i) + ci + mask.substring(i + 1);
                        }
                    }
                }
                result.setMask(mask);
            }
        }

        return result;
    }
}

